package com.app.twth.ui.news;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.app.twth.databinding.ItemNewsListBinding;
import com.app.twth.interfaces.MyOnClickListener;
import com.app.twth.utils.Utils;

import java.util.List;


public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.MyViewHolder> {
    private int index = -1;
    private List<NewsBean> data;
    private Context mContext;
    private LayoutInflater mInflater;
    private MyOnClickListener myOnClickListener;

    public NewsAdapter(Context mContext, List<NewsBean> data, MyOnClickListener myOnClickListener) {
        this.mContext = mContext;
        this.data = data;
        this.mInflater = LayoutInflater.from(mContext);
        this.myOnClickListener = myOnClickListener;
    }

    public void setData(List<NewsBean> data, int index) {
        this.data = data;
        this.index = index;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(ItemNewsListBinding.inflate(mInflater, parent, false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.setData(position);
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ItemNewsListBinding itemBinding;

        public MyViewHolder(ItemNewsListBinding itemBinding) {
            super(itemBinding.getRoot());
            this.itemBinding = itemBinding;
        }

        public void setData(int position) {
            itemView.setTag(position);
            NewsBean task = data.get(position);
            itemBinding.tvUpdatedDatetime.setText(task.getUpdatedDatetime() + "");
            int type = task.getType();
            switch (type) {
                case 1:
                    itemBinding.tvTaskType.setText("Re-Tally");
                    break;
                case 2:
                    itemBinding.tvTaskType.setText("Re-Measure");
                    break;
                case 3:
                    itemBinding.tvTaskType.setText("Re-Check");
                    break;
                case 4:
                    itemBinding.tvTaskType.setText("Damage(Destroy)");
                    break;
                case 5:
                    itemBinding.tvTaskType.setText("Damage(Putatay)");
                    break;
                case 6:
                    itemBinding.tvTaskType.setText("Damage(New SKU)");
                    break;
                case 7:
                    itemBinding.tvTaskType.setText("Final-Process");
                    break;
                default:
                    itemBinding.tvTaskType.setText("--");
                    break;
            }

            setGone2Visible(task.getContainerNo(), itemBinding.tvContainerNo, itemBinding.tvContainerNoHint);
            setGone2Visible(task.getSku(), itemBinding.tvSku, itemBinding.tvSkuHint);
            setGone2Visible(task.getIbLpn(), itemBinding.tvIblpn, itemBinding.tvIblpnHint);
            setGone2Visible(task.getBatchNo(), itemBinding.tvBatch, itemBinding.tvBatchHint);

            String status = task.getStatus();
            if (!Utils.isEmpty(status) && status.equals("1")) {
                itemBinding.ivGo.setVisibility(View.VISIBLE);
            } else {
                itemBinding.ivGo.setVisibility(View.GONE);
            }
            itemBinding.layout.setOnClickListener(v -> myOnClickListener.OnClick(position));
        }
    }

    public void setGone2Visible(String value, TextView tv, TextView tvHint) {
        if (Utils.isEmpty(value)) {
            tvHint.setVisibility(View.GONE);
            tv.setVisibility(View.GONE);
        } else {
            tvHint.setVisibility(View.VISIBLE);
            tv.setVisibility(View.VISIBLE);
            tv.setText(value + "");
        }
    }
}
